package com.hd.crdao.activity;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.SmsCheckInfoBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.CheckUtils;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.TimeDownCounter;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/5/12 0012
 */
public class CheckActivity extends BaseSecondActivity {
    private static final int GET_TELPHONE_CODE = 101;

    public String mPhonenumber;
    @BindView(R.id.preordercheck)
    ExpandHeadView mPreordercheck;
    @BindView(R.id.etphone)
    EditText mEtphone;
    @BindView(R.id.llphone)
    LinearLayout mLlphone;
    @BindView(R.id.check_checkcode)
    EditText mCheckCheckcode;
    @BindView(R.id.getcheckcode)
    Button mGetcheckcode;
    @BindView(R.id.llcheckcode)
    RelativeLayout mLlcheckcode;
    @BindView(R.id.checksubmit)
    Button mChecksubmit;
    @BindView(R.id.ll_phone)
    LinearLayout mLlPhone;
    public TimeDownCounter mTimeDownCounter;
    private boolean isPassed;

    @Override
    protected void initView() {
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_check;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mPreordercheck;
    }

    /**
     * 打电话给服务中心
     */
    private void TakePhoneToSerivce() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+getResources().getString(R.string.companyTelephone)));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }


    /**
     * 检查手机号码输入是否正确
     */
    private boolean checkPhoneNumber() {
        mPhonenumber = mEtphone.getText().toString().trim();
        if (!TextUtils.isEmpty(mPhonenumber) && CheckUtils.isMobileNO(mPhonenumber)) {
            return true;
        } else {
            MyToast.show("请输入正确的手机号码");
            return false;
        }
    }

    /**
     * 输入手机号码，输入验证码
     */
    private boolean TakeCheckCode() {
        if (!checkPhoneNumber()) {
            return false;
        }

        OkHttpUtils.post().url(Constants.GETSMSCODE_URL)
                .addParams("mobile", mPhonenumber)
                .addParams("smsType", "3")
                .addParams("sign", Constants.SIGN)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                SmsCheckInfoBean smsCheckInfoBean = gson.fromJson(response, SmsCheckInfoBean.class);
                if (smsCheckInfoBean.getSuccess() == 0) {
                    MyToast.show("发送成功");
                    mTimeDownCounter = new TimeDownCounter(60000, 1000, mGetcheckcode);
                    mTimeDownCounter.start();
                    isPassed = true;
                } else {
                    MyToast.show(smsCheckInfoBean.getMessage());
                    isPassed = false;
                }
            }
        });
        return isPassed;
    }

    /**
     * 验证短信验证码
     */
    public void checkSmsCode() {
        String smsCode = mCheckCheckcode.getText().toString().trim();
        OkHttpUtils.post().url(Constants.ORDERCHECK_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("mobile", mPhonenumber)
                .addParams("smsCode", smsCode)
             //   .addParams("smsType", "3")  //预约检测
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                SmsCheckInfoBean smsCheckInfoBean = gson.fromJson(response, SmsCheckInfoBean.class);
                if (smsCheckInfoBean.getSuccess() == 0) {
                    MyToast.show("预约成功");
                    finish();
                } else {
                    MyToast.show(smsCheckInfoBean.getMessage());
                }
            }
        });
    }


    @OnClick({R.id.getcheckcode, R.id.checksubmit, R.id.ll_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.getcheckcode:     //获取验证码
                TakeCheckCode();
                break;
            case R.id.checksubmit:          //立即提交
                if (TakeCheckCode()) {
                    checkSmsCode();
                }

                break;
            case R.id.ll_phone:             //给服务中心打电话
                TakePhoneToSerivce();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimeDownCounter != null) {
            mTimeDownCounter.cancel();
            mTimeDownCounter = null;
        }
    }
}